package com.inspinia.base.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * <p>TypeChange.<a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>
 * <p>JAVA基本类型互相转换
 *
 * @author heardy
 * @version 1.0
 * @time 2011/01/30
 */
public class TypeChange {

    /**
     * 转为字符串
     *
     * @param obj
     * @return 为null时返回空字符串
     */
    public static String objectToString(Object obj) {
        String str = "";
        try {
            str = (String) obj;
            if (str == null) {
                str = "";
            }
        } catch (ClassCastException ce) {
            try {
                str = String.valueOf(obj);
            } catch (Exception e) {
                str = "";
            }
        }
        return str.trim();
    }

    /**
     * 转null为空字符
     *
     * @return
     */
    public static String nullOfString(Object obj) {
        return objectToString(obj);
    }

    /**
     * 转null为Int
     *
     * @param obj
     * @return
     */
    public static int nullOfInt(Object obj) {
        return objectToInt(obj);
    }

    // *****************Object 转 其他类型*****************

    /**
     * 转字节
     *
     * @return
     */
    public static byte objectToByte(Object obj) {
        String str = objectToString(obj);
        str = nullOfString(str);
        try {
            return Byte.parseByte(str);
        } catch (NumberFormatException e) {
            System.out.println("转换出错" + e);
            throw e;
        }
    }

    /**
     * 转布尔
     *
     * @return 为1或"true"返回真 否则假
     */
    public static boolean objectToBoolean(Object obj) {
        String str = objectToString(obj);
        if (str.equals("1")) {
            return true;
        } else if (str.equals("0")) {
            return false;
        } else {
            try {
                return Boolean.parseBoolean(str);
            } catch (Exception e) {
                return false;
            }
        }
    }

    /**
     * 转Int
     *
     * @return 相应的值 或 零
     */
    public static int objectToInt(Object obj) {
        String str = objectToString(obj);
        int i = 0;
        try {
            i = Integer.parseInt(str.trim());
        } catch (NumberFormatException e) {
            i = 0;
        }
        return i;
    }

    /**
     * 转short
     *
     * @param obj
     * @return 相应的值 或 零
     */
    public static short objectToShort(Object obj) {
        String str = objectToString(obj);
        short i = 0;
        try {
            i = Short.parseShort(str.trim());
        } catch (NumberFormatException e) {
            i = 0;
        }
        return i;
    }

    /**
     * 转Double
     *
     * @param obj
     * @return 相应的值 或 零
     */
    public static double objectToDouble(Object obj) {
        String str = objectToString(obj);
        double i = 0;
        try {
            i = Double.parseDouble(str.trim());
        } catch (NumberFormatException e) {
            i = 0;
        }
        return i;
    }

    /**
     * 转Long
     *
     * @param obj
     * @return 相应的值 或 零
     */
    public static long objectToLong(Object obj) {
        String str = objectToString(obj);
        Long li = new Long(0);
        try {
            li = Long.valueOf(str);
        } catch (NumberFormatException e) {
        }
        return li.longValue();
    }

    // ***************** 其他类型相互转换 *****************

    /**
     * double转long
     *
     * @param d
     * @return 只截取前面的整数
     */
    public static long doubleToLong(double d) {
        long l = 0;
        try {
            // double转换成long前要过滤掉double类型小数点后数据
            l = Long.parseLong(String.valueOf(d).substring(0,
                    String.valueOf(d).lastIndexOf(".")));
        } catch (Exception e) {
            l = 0;
        }
        return l;
    }

    /**
     * double转int
     *
     * @param d
     * @return 只截取前面的整数
     */
    public static int doubleToInt(double d) {
        int i = 0;
        try {
            // double转换成Int前要过滤掉double类型小数点后数据
            i = Integer.parseInt(String.valueOf(d).substring(0,
                    String.valueOf(d).lastIndexOf(".")));
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    /**
     * double转long(四舍五入)
     *
     * @param d
     * @return 只截取前面的整数 (四舍五入)
     */
    public static long doubleToLongWhithRound(double d) {
        long l = 0;
        try {
            l = Math.round(d);
        } catch (Exception e) {
            l = 0;
        }
        return l;
    }

    /**
     * double转int(四舍五入)
     *
     * @param d
     * @return 只截取前面的整数 (四舍五入)
     */
    public static int doubleToIntWhithRound(double d) {
        int i = 0;
        try {
            i = (int) Math.round(d);
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    public static double longToDouble(long d) {
        double l = 0;
        try {
            l = Double.parseDouble(String.valueOf(d));
        } catch (Exception e) {
            l = 0;
        }
        return l;
    }

    public static int longToInt(long d) {
        int l = 0;
        try {
            l = Integer.parseInt(String.valueOf(d));
        } catch (Exception e) {
            l = 0;
        }
        return l;
    }

    public static Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return obj;


    }

    public static byte[] ObjectToByte(java.lang.Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return bytes;
    }
}